package com.um.jdy.app.device.manager;

import cn.hutool.core.util.StrUtil;
import com.um.jdy.common.device.entity.po.Gun;
import com.um.jdy.common.device.entity.po.Pile;
import com.um.jdy.common.device.enums.DeviceEnum;
import com.um.jdy.common.device.service.GunService;
import com.um.jdy.common.device.service.OnlineLogService;
import com.um.jdy.common.device.service.PileService;
import com.um.jdy.common.push.service.PushService;
import com.um.jdy.rpc.entity.EVDeviceInfo;
import com.um.jdy.rpc.entity.EVPort;
import com.um.jdy.rpc.ev.confirmation.RemoteUpgradeConfirmation;
import com.um.jdy.rpc.ev.entity.BmsInfoData;
import com.um.jdy.rpc.ev.entity.BmsSettingsData;
import com.um.jdy.rpc.factory.ServiceFactory;
import com.um.jdy.rpc.notify.RpcEVBmsNotifyService;
import com.um.jdy.rpc.notify.RpcEVDeviceNotifyService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 汽车桩设备处理逻辑
 * @Description
 * @Author dnannhui
 * @Date 2023/5/22 10:37
 */
@Slf4j
@Service
@DubboService
public class EVDeviceManager implements RpcEVDeviceNotifyService {

    @Autowired
    PileService pileService;

    @Autowired
    GunService gunService;

    @Autowired
    OnlineLogService onlineLogService;

    @Autowired
    ServiceFactory serviceFactory;

    @Autowired
    PushService pushService;

    /**
     * 设备登录上报
     *
     * @param seq   序列号
     * @param imei  IMEI码
     * @param ports 端口列表
     * @param info  设备信息
     */
    @Override
    public boolean reqDeviceLogin(String seq, String imei, List<EVPort> ports, EVDeviceInfo info) {
        Pile devicePile = pileService.getRow("imei",imei);
        if(devicePile == null)
            return false;

        onlineLogService.addOnline(devicePile.getCode(),devicePile.getName());

        //修改为在线状态,上报状态正常,更新上线时间
        devicePile.setRssi(info.getRssi());
        devicePile.setIccid(info.getIccid());
        devicePile.setVersion(info.getVersion());
        devicePile.setIpAddress(info.getIpAddress());
        devicePile.setOpenState(DeviceEnum.OpenStatus.Enable.name());
        devicePile.setDeviceStatus(DeviceEnum.DeviceStatus.Online.name());
        devicePile.setLastOnlineTime(new Date());
        devicePile.setProtocol(info.getProtocol());
        if(devicePile.getParameters() == null || StrUtil.isEmpty(devicePile.getParameters())){
            devicePile.setParameters(serviceFactory.getDeviceParameters(devicePile.getDeviceType(),info.getProtocol()));
        }
        pileService.updateRow(devicePile);

        return true;
    }

    /**
     * 设备心跳上报
     *
     * @param seq  序列号
     * @param imei IMEI码
     */
    @Override
    public void reqDeviceHeart(String seq, String imei,String protocol) {
        Pile devicePile = pileService.getRow("imei",imei);
        if (devicePile == null)
            return;

        devicePile.setProtocol(protocol);
        devicePile.setDeviceStatus(DeviceEnum.DeviceStatus.Online.name());
        devicePile.setHeartTime(new Date());
        pileService.updateRow(devicePile);

    }

    /**
     * 设备离线上报
     * @param seq 序列号
     * @param imei IMEI码
     */
    @Override
    public void reqDeviceOffline(String seq, String imei){
        Pile devicePile = pileService.getRow("imei",imei);
        if (devicePile != null && devicePile.getDeviceStatus().equals(DeviceEnum.DeviceStatus.Online.name())) {
            devicePile.setDeviceStatus(DeviceEnum.DeviceStatus.Offline.name());
            devicePile.setLastOfflineTime(new Date());
            pileService.updateRow(devicePile);

            onlineLogService.addOffline(devicePile.getCode(),devicePile.getName());

            List<Gun> guns = gunService.getRows("device_pile_id",devicePile.getDevicePileId());
            for(Gun gun : guns){
                gun.setStatus(DeviceEnum.PortStatus.Offline.name());
                gunService.updateRow(gun);
            }

            // 推送
            pushService.pushDeviceOffline(devicePile);
        }
    }

    /**
     * 桩回复升级状态
     * @param seq
     * @param imei IMEI码
     * @param confirmation 回复数据
     */
    @Override
    public void respUpgrade(String seq, String imei, RemoteUpgradeConfirmation confirmation){
        //TODO 暂未实现
    }


}
